home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / PCCP024.ARJ / XMCRC1KS.C < prev    next >
Text File  |  1992-05-17  |  4KB  |  237 lines

  1. /*    Copyright (C) 1992 Peter Edward Cann, all rights reserved.
  2.  *    MicroSoft QuickC
  3.  */
  4.  
  5. #include<stdio.h>
  6. #include<bios.h>
  7. #include<dos.h>
  8. #include<fcntl.h>
  9. #include<sys\types.h>
  10. #include<sys\stat.h>
  11. #include<signal.h>
  12. #include"port.h"
  13.  
  14. #define NAK 21
  15. #define ACK 6
  16. #define SOH 1
  17. #define STX 2
  18. #define EOT 4
  19. #define CAN 24
  20.  
  21. sendchar(c)
  22.     unsigned char c;
  23.     {
  24.     while(!((inp(basereg+STATREG)&TXMTMASK)&&(inp(basereg+MSTATREG)&CTSMASK)));
  25.     outp(basereg, c);
  26.     }
  27.  
  28. int follow;
  29.  
  30. int rcharto(ticks)
  31.     int ticks;
  32.     {
  33.     long tstamp, tstamp1, dayofticksp;
  34.     int c;
  35.     _bios_timeofday(_TIME_GETCLOCK, &tstamp);
  36.     dayofticksp=0;
  37.     while(1)
  38.         {
  39.         if(_bios_timeofday(_TIME_GETCLOCK, &tstamp1))
  40.             dayofticksp+=20*60*60*24;
  41.         if(tstamp1+dayofticksp-tstamp>ticks)
  42.             return(-1); /* NOTE: This is an INT!!! */
  43.         if(follow!=index)
  44.             {
  45.             c=buf[follow++];
  46.             follow=follow%TBUFSIZ;
  47.             return(c);
  48.             }
  49.         }
  50.     }
  51.  
  52. int calccrc(ptr, count)
  53.     char *ptr;
  54.     int count;
  55.     {
  56.     int crc, i;
  57.     crc = 0;
  58.     while(--count >= 0)
  59.         {
  60.         crc = crc ^ (int)*ptr++ << 8;
  61.         for(i = 0; i < 8; ++i)
  62.             if(crc & 0x8000)
  63.                 crc = crc << 1 ^ 0x1021;
  64.             else
  65.                 crc = crc << 1;
  66.         }
  67.     return (crc & 0xFFFF);
  68.     }
  69.  
  70. unsigned char block[1024];
  71.  
  72. sblock(blockn)
  73.     int blockn;
  74.     {
  75.     unsigned char c;
  76.     unsigned short crc, rcrc;
  77.     int i;
  78.     crc=calccrc(block, 1024);
  79.     sendchar(STX);
  80.     sendchar(blockn);
  81.     sendchar((blockn^0xff)&0xff);
  82.     for(i=0;i<1024;++i)
  83.         sendchar(block[i]);
  84.     sendchar((crc>>8)&0xff);
  85.     sendchar(crc&0xff);
  86.     }
  87.  
  88. unsigned char shortblock[128];
  89.  
  90. ssblock(blockn)
  91.     int blockn;
  92.     {
  93.     unsigned char c;
  94.     unsigned short crc, rcrc;
  95.     int i;
  96.     crc=calccrc(shortblock, 128);
  97.     sendchar(SOH);
  98.     sendchar(blockn);
  99.     sendchar((blockn^0xff)&0xff);
  100.     for(i=0;i<128;++i)
  101.         sendchar(shortblock[i]);
  102.     sendchar((crc>>8)&0xff);
  103.     sendchar(crc&0xff);
  104.     }
  105.  
  106. quit()
  107.     {
  108.     cleanup();
  109.     exit(99);
  110.     }
  111.  
  112. main(argc, argv)
  113.     int argc;
  114.     char **argv;
  115.     {
  116.     int i, j, k, l, infd, ok, c;
  117.     unsigned char *blkptr;
  118.     unsigned char blocknum;
  119.     long nbytes;
  120.     index=follow=0;
  121.     printf("Copyright (C) 1992 Peter Edward Cann, all rights reserved.\n");
  122.     printf("xmodem crc 1k send of %s.\n", argv[4]);
  123.     if(argc!=5)
  124.         {
  125.         printf("USAGE: xmodemr <comnum> <bps> <stopbits> <file pathname>\n");
  126.         exit(1);
  127.         }
  128.     if((infd=open(argv[4], O_RDONLY|O_BINARY))==-1)
  129.         {
  130.         printf("Error opening file %s.\n", argv[4]);
  131.         exit(2);
  132.         }
  133.     comnum=atoi(argv[1])-1;
  134.     speed=atoi(argv[2]);
  135.     databits='8';
  136.     parity='n';
  137.     stopbits=argv[3][0];
  138.     setport();
  139.     signal(SIGINT, quit);
  140.     readset();
  141.     setup();
  142.     nbytes=0;
  143.     if(rcharto(2000)!='C')
  144.         {
  145.         printf("Spurrious char or no C in 100 seconds.\n");
  146.         cleanup();
  147.         exit(10);
  148.         }
  149.     blocknum=1;
  150.     while(1)
  151.         {
  152.         if((j=read(infd, block, 1024))==0)
  153.             {
  154.             printf("\nEnd of file.\n");
  155.             sendchar(EOT);
  156.             do
  157.                 c=rcharto(300);
  158.             while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  159.             if(c!=ACK)
  160.                 {
  161.                 printf("No ACK of EOT.\n");
  162.                 cleanup();
  163.                 exit(13);
  164.                 }
  165.             else
  166.                 {
  167.                 printf("Successful.\n");
  168.                 cleanup();
  169.                 exit(0);
  170.                 }
  171.             }
  172.         for(c=j;c<1024;c++)
  173.             block[c]=26;
  174.         if(j>896)
  175.             {
  176.             i=0;
  177.             do
  178.                 {
  179.                 printf("\nSending block %d. ", blocknum);
  180.                 sblock(blocknum);
  181.                 do
  182.                     c=rcharto(200);
  183.                 while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  184.                 }
  185.             while((c==NAK)&&(i++<10));
  186.             if(c==ACK)
  187.                 {
  188.                 blocknum++;
  189.                 nbytes+=1024;
  190.                 printf("Successful. Bytes so far: %ld", nbytes);
  191.                 }
  192.             }
  193.         else
  194.             {
  195.             for(k=0;k<(j+128);k+=128)
  196.                 {
  197.                 i=0;
  198.                 do
  199.                     {
  200.                     for(l=0;l<128;++l)
  201.                         shortblock[l]=block[k+l];
  202.                     printf("\nSending block %d. ", blocknum);
  203.                     ssblock(blocknum);
  204.                     do
  205.                         c=rcharto(200);
  206.                     while((c!=ACK)&&(c!=NAK)&&(c!=CAN)&&(c!=-1));
  207.                     }
  208.                 while((c==NAK)&&(i++<10));
  209.                 if(c!=ACK)
  210.                     break;
  211.                 else
  212.                     {
  213.                     blocknum++;
  214.                     nbytes+=128;
  215.                     printf("Successful. Bytes so far: %ld", nbytes);
  216.                     }
  217.                 }
  218.             }
  219.         if(c!=ACK)
  220.             if(c==NAK)
  221.                 {
  222.                 printf("\nRetry limit exceeded.\n");
  223.                 cleanup();
  224.                 exit(14);
  225.                 }
  226.             else
  227.                 {
  228.                 printf("\nSpurrious character hex %02x; ACK or NAK expected.\n", c);
  229.                 cleanup();
  230.                 exit(11);
  231.                 }
  232.         }
  233.     printf("Programming error; fell through end; see code.\n");
  234.     cleanup();
  235.     exit(12);
  236.     }
  237.